<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>039.JAVA Socket网络编程概要 | 似南-全栈开发路上的探索者、追梦人</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  
  
  <meta name="keywords" content="似南,FaithSinan" />
  
  <meta name="description" content="JAVA中的网络支持针对网络通信的不同层次，JAVA提供的网络功能有四大类：  InetAddress：用于标识网络上的硬件资源。 URL：统一资源定位符，通过URL可以直接读取或写入网络上的数据。 Sockets：使用TCP协议实现网络通信的Socket相关的类。 Datagram：使用UDP协议，将数据保存在数据报中，通过网络进行通信。">
<meta property="og:type" content="article">
<meta property="og:title" content="039.JAVA Socket网络编程概要">
<meta property="og:url" content="http://yoursite.com/2020/10/27/039.JAVA%20Socket%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B%E6%A6%82%E8%A6%81/index.html">
<meta property="og:site_name" content="似南-全栈开发路上的探索者、追梦人">
<meta property="og:description" content="JAVA中的网络支持针对网络通信的不同层次，JAVA提供的网络功能有四大类：  InetAddress：用于标识网络上的硬件资源。 URL：统一资源定位符，通过URL可以直接读取或写入网络上的数据。 Sockets：使用TCP协议实现网络通信的Socket相关的类。 Datagram：使用UDP协议，将数据保存在数据报中，通过网络进行通信。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-10-27T10:24:30.000Z">
<meta property="article:modified_time" content="2020-10-30T07:29:24.237Z">
<meta property="article:author" content="FaithSinan">
<meta property="article:tag" content="Socket">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="似南-全栈开发路上的探索者、追梦人" type="application/atom+xml">
  
  
    <link rel="icon" href="/images/avatar.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

  
<link rel="stylesheet" href="/css/highlight.css">

<meta name="generator" content="Hexo 4.2.1"></head>

<body>
  <div id="fullpage" class="mobile-nav-right">
    
      <div id="wrapper" title="图片来自网络">
    
    
      <header id="header">
  <div id="nav-toggle" class="nav-toggle"></div>
  <div class="head-box global-width">
    <nav class="nav-box nav-left">
      
        <a class="nav-item" href="/" title
        
        >首页</a>
      
        <a class="nav-item" href="/archives" title
        
        >归档</a>
      
    </nav>
  </div>
</header>
      <div id="middlecontent" title class="global-width sidebar-left">
        <section id="main"><article id="post-039.JAVA Socket网络编程概要" class="article global-container article-type-post" itemscope itemprop="blogPost">
  
    <header class="article-header">
      
  
    <h1 class="article-title" itemprop="name">
      039.JAVA Socket网络编程概要
    </h1>
  

    </header>
  
  <div class="article-meta">
    <a href="/2020/10/27/039.JAVA%20Socket%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B%E6%A6%82%E8%A6%81/" class="article-date">
  <time datetime="2020-10-27T10:24:30.000Z" itemprop="datePublished">2020-10-27</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/Web%E5%85%A8%E6%A0%88%E6%9E%B6%E6%9E%84/">Web全栈架构</a>►<a class="article-category-link" href="/categories/Web%E5%85%A8%E6%A0%88%E6%9E%B6%E6%9E%84/Java/">Java</a>
  </div>

    
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Socket/" rel="tag">Socket</a></li></ul>

  </div>
  
    <span id="busuanzi_container_page_pv">
      本文总阅读量<span id="busuanzi_value_page_pv"></span>次
    </span>
  

  <div class="article-inner">
    
    <div class="article-content article-content-cloud" itemprop="articleBody">
      
        <h4 id="JAVA中的网络支持"><a href="#JAVA中的网络支持" class="headerlink" title="JAVA中的网络支持"></a>JAVA中的网络支持</h4><p>针对网络通信的不同层次，JAVA提供的网络功能有四大类：</p>
<ul>
<li>InetAddress：用于标识网络上的硬件资源。</li>
<li>URL：统一资源定位符，通过URL可以直接读取或写入网络上的数据。</li>
<li>Sockets：使用TCP协议实现网络通信的Socket相关的类。</li>
<li>Datagram：使用UDP协议，将数据保存在数据报中，通过网络进行通信。</li>
</ul>
<a id="more"></a>

<h4 id="InetAddress类使用示例"><a href="#InetAddress类使用示例" class="headerlink" title="InetAddress类使用示例"></a>InetAddress类使用示例</h4><p>获取主机的软硬件信息。</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="JAVA"><figure class="iseeu highlight /java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.net.InetAddress;</span><br><span class="line"><span class="keyword">import</span> java.net.UnknownHostException;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SocketDome</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            InetAddress address = InetAddress.getLocalHost();   <span class="comment">//获取本机的InetAddress实例</span></span><br><span class="line">            </span><br><span class="line">            System.out.println(<span class="string">"计算机名："</span>+address.getHostName());</span><br><span class="line">            System.out.println(<span class="string">"IP地址："</span>+address.getHostAddress());</span><br><span class="line">            System.out.println(<span class="string">"获取的本地InerAddress对象："</span>+address);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (UnknownHostException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">            System.out.println(<span class="string">"程序异常："</span>+e);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>



<h4 id="URL类使用示例"><a href="#URL类使用示例" class="headerlink" title="URL类使用示例"></a>URL类使用示例</h4><p>通过URL对象的openStream()方法可以得到指定的输入流，通过输入流可以读取、访问网络上的数据。</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="JAVA"><figure class="iseeu highlight /java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取URL对应的资源</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">urlDome</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            URL baidu = <span class="keyword">new</span> URL(<span class="string">"http://www.baidu.com"</span>);   <span class="comment">//创建一个URL实例</span></span><br><span class="line">            InputStream is = baidu.openStream();                <span class="comment">//获取URL对象所表示资源的字节输入流</span></span><br><span class="line">            InputStreamReader isr = <span class="keyword">new</span> InputStreamReader(is);  <span class="comment">//将字节输入流转换为字符输入流</span></span><br><span class="line">            BufferedReader br = <span class="keyword">new</span> BufferedReader(isr);        <span class="comment">//为字符输入流添加缓冲</span></span><br><span class="line">            <span class="keyword">int</span> i=<span class="number">0</span>;</span><br><span class="line">            <span class="keyword">while</span> (br.readLine()!=<span class="keyword">null</span>)&#123;</span><br><span class="line">                System.out.println(br.readLine());      <span class="comment">//按行输出获取到的字符</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (MalformedURLException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure></div>



<h4 id="Socket通信原理及步骤"><a href="#Socket通信原理及步骤" class="headerlink" title="Socket通信原理及步骤"></a>Socket通信原理及步骤</h4><p>​        Socket是基于TCP协议实现网络通信的类。TCP协议是面向连接、可靠的、有序的，以字节流的方式发送数据。</p>
<p>​    客户端封装Socket类，服务端封装ServerSocket类。</p>
<p>Socket通信实现步骤：</p>
<ul>
<li>创建ServerSocket和Socket。</li>
<li>打开连接到Socket的输入/输出流。</li>
<li>按照协议对Socket进行读/写操作。</li>
<li>关闭输入输出流，关闭Socket。</li>
</ul>
<p>服务器端：</p>
<p>1、创建ServerSocket对象，绑定监听端口。</p>
<p>2、通过accept()方法监听客户端连接请求。</p>
<p>3、连接建立后，通过输入流读取客户端发送的请求消息。</p>
<p>4、通过输出流向客户端发送响应消息。</p>
<p>5、关闭相关资源。</p>
<p>客户端：</p>
<p>1、创建Socket对象，指明需要连接的服务器的地址和端口号。</p>
<p>2、连接建立后，通过输出流向服务器端发送请求信息。</p>
<p>3、通过输入流获取服务器响应的信息。</p>
<p>4、关闭相关资源。</p>
<h5 id="Socket会话实例："><a href="#Socket会话实例：" class="headerlink" title="Socket会话实例："></a>Socket会话实例：</h5><p>服务端：</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="JAVA"><figure class="iseeu highlight /java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.*;</span><br><span class="line"><span class="keyword">import</span> java.net.ServerSocket;</span><br><span class="line"><span class="keyword">import</span> java.net.Socket;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Server</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">serverImpl</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">//创建服务端Socket</span></span><br><span class="line">            ServerSocket listenSocket = <span class="keyword">new</span> ServerSocket(<span class="number">2024</span>); <span class="comment">//创建一个服务端Socket，并绑定监听端口</span></span><br><span class="line">            System.out.println(<span class="string">"***服务器即将启动，等待客户端的连接***"</span>);</span><br><span class="line"></span><br><span class="line">            <span class="comment">//处理客户端发来的连接请求，生成客户端Socket对象</span></span><br><span class="line">            Socket clientSocket =  listenSocket.accept();   <span class="comment">//服务器阻塞等待，直到获取并接受到客户端的连接请求</span></span><br><span class="line">            System.out.println(<span class="string">"客户端连接成功！客户机地址为："</span>+clientSocket.getRemoteSocketAddress());</span><br><span class="line"></span><br><span class="line">            <span class="comment">//获取输入流，以获取客户端发来的数据</span></span><br><span class="line">            InputStream input = clientSocket.getInputStream();    <span class="comment">//从clientSocket生成输入流。</span></span><br><span class="line">            InputStreamReader inputReader = <span class="keyword">new</span> InputStreamReader(input);   <span class="comment">//将字节流转换为对中文兼容好的字符流</span></span><br><span class="line">            BufferedReader bufferedReader = <span class="keyword">new</span> BufferedReader(inputReader);    <span class="comment">//创建与字符流配套的缓冲流</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">//读取客户端发来的数据</span></span><br><span class="line">            String clientInfo = <span class="keyword">null</span>;</span><br><span class="line">            <span class="keyword">while</span> ((clientInfo = bufferedReader.readLine()) != <span class="keyword">null</span>)  <span class="comment">//循环读取文本行</span></span><br><span class="line">            System.out.println(<span class="string">"客户端数据说："</span>+clientInfo);</span><br><span class="line"></span><br><span class="line">            <span class="comment">//获取输出流</span></span><br><span class="line">            OutputStream output = clientSocket.getOutputStream();   <span class="comment">//获取输出流</span></span><br><span class="line">            OutputStreamWriter outputWriter = <span class="keyword">new</span> OutputStreamWriter(output);   <span class="comment">//将字节流转换为字符流</span></span><br><span class="line">            BufferedWriter bufferedWriter = <span class="keyword">new</span> BufferedWriter(outputWriter);   <span class="comment">//为字符流添加缓冲</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">//响应客户端会话</span></span><br><span class="line">            String replayData = <span class="string">"你好！我是服务器，很高兴认识你"</span>;</span><br><span class="line">            bufferedWriter.write(replayData);   <span class="comment">//写入回复的数据</span></span><br><span class="line">            bufferedWriter.flush();     <span class="comment">//刷新该流的缓冲</span></span><br><span class="line">            System.out.println(<span class="string">"已回复客户端会话："</span>+replayData);</span><br><span class="line"></span><br><span class="line">            <span class="comment">//关闭相关资源</span></span><br><span class="line">            <span class="keyword">if</span> (bufferedReader != <span class="keyword">null</span>)</span><br><span class="line">                bufferedReader.close();</span><br><span class="line">            <span class="keyword">if</span> (clientSocket != <span class="keyword">null</span>)</span><br><span class="line">                clientSocket.close();</span><br><span class="line">            <span class="keyword">if</span> (listenSocket != <span class="keyword">null</span>)</span><br><span class="line">                listenSocket.close();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Server server = <span class="keyword">new</span> Server();</span><br><span class="line">        server.serverImpl();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>



<p>客户端：</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="JAVA"><figure class="iseeu highlight /java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.*;</span><br><span class="line"><span class="keyword">import</span> java.net.Socket;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Client</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">clientImpl</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">//创建客户端Socket，并指定要连接的服务器地址和端口</span></span><br><span class="line">            Socket clientSocket = <span class="keyword">new</span> Socket(<span class="string">"127.0.0.1"</span>,<span class="number">2024</span>);</span><br><span class="line">            <span class="comment">//获取输出流</span></span><br><span class="line">            OutputStream output = clientSocket.getOutputStream();   <span class="comment">//从socket生成字节输出流</span></span><br><span class="line">            OutputStreamWriter outputWriter = <span class="keyword">new</span> OutputStreamWriter(output);   <span class="comment">//将字节流转换为字符流</span></span><br><span class="line">            BufferedWriter bufferedWriter = <span class="keyword">new</span> BufferedWriter(outputWriter);   <span class="comment">//为字符流添加配套的缓冲</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">//向服务器端发送消息</span></span><br><span class="line">            String Data = <span class="string">"服务器你好，我是客户端！"</span>;</span><br><span class="line">            bufferedWriter.write(Data);   <span class="comment">//写入数据</span></span><br><span class="line">            bufferedWriter.flush();     <span class="comment">//向服务器发送数据</span></span><br><span class="line">            System.out.println(<span class="string">"已对服务器说："</span>+Data);</span><br><span class="line"></span><br><span class="line">            clientSocket.shutdownOutput();  <span class="comment">//关闭输出流</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">//获取输入流</span></span><br><span class="line">            InputStream input = clientSocket.getInputStream();</span><br><span class="line">            InputStreamReader inputReader = <span class="keyword">new</span> InputStreamReader(input);</span><br><span class="line">            BufferedReader bufferedReader = <span class="keyword">new</span> BufferedReader(inputReader);</span><br><span class="line"></span><br><span class="line">            <span class="comment">//读取服务端响应的数据</span></span><br><span class="line">            String result = <span class="keyword">null</span>;</span><br><span class="line">            <span class="keyword">while</span> ((result = bufferedReader.readLine()) != <span class="keyword">null</span>)</span><br><span class="line">                System.out.println(<span class="string">"服务器回复的消息："</span>+result);</span><br><span class="line"></span><br><span class="line">            <span class="comment">//关闭相关资源</span></span><br><span class="line">            <span class="keyword">if</span> (bufferedReader != <span class="keyword">null</span>)</span><br><span class="line">                bufferedReader.close();</span><br><span class="line">            <span class="keyword">if</span> (bufferedWriter != <span class="keyword">null</span>)</span><br><span class="line">                bufferedWriter.close();</span><br><span class="line">            <span class="keyword">if</span> (clientSocket != <span class="keyword">null</span>)</span><br><span class="line">                clientSocket.close();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Client client = <span class="keyword">new</span> Client();</span><br><span class="line">        client.clientImpl();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>



<h4 id="应用多线程实现服务端与多客户端通信"><a href="#应用多线程实现服务端与多客户端通信" class="headerlink" title="应用多线程实现服务端与多客户端通信"></a>应用多线程实现服务端与多客户端通信</h4><p>大多数场景下，服务端都需与多客户端进行通信，由此便需要用到多线程技术，实现与多客户端的通信。</p>
<p>基本步骤：</p>
<p>1、服务器端创建ServerSocket，循环调用accept()等待客户端连接。</p>
<p>2、客户端创建一个socket并请求和服务器端连接。</p>
<p>3、服务器端接受客户端请求，创建socket与该客户建立专线连接。</p>
<p>4、建立连接的两个socket在一个单独的线程上对话。</p>
<p>5、服务器端继续等待新连接。</p>
<p>实现多线程只需在上面例子的基础上修改服务端代码即可。</p>
<p>修改后的Server类代码为：</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="JAVA"><figure class="iseeu highlight /java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.*;</span><br><span class="line"><span class="keyword">import</span> java.net.ServerSocket;</span><br><span class="line"><span class="keyword">import</span> java.net.Socket;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Server</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">serverImpl</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">//创建服务端Socket</span></span><br><span class="line">            ServerSocket listenSocket = <span class="keyword">new</span> ServerSocket(<span class="number">2024</span>); <span class="comment">//创建一个服务端Socket，并绑定监听端口</span></span><br><span class="line">            System.out.println(<span class="string">"***服务器即将启动，等待客户端的连接***"</span>);</span><br><span class="line"></span><br><span class="line">            <span class="comment">//处理客户端发来的连接请求，生成客户端Socket对象</span></span><br><span class="line">            Socket clientSocket = <span class="keyword">null</span>;</span><br><span class="line"></span><br><span class="line">            <span class="comment">//设置一个死循环，调用accpet方法循环监听，等待客户端连接</span></span><br><span class="line">            <span class="keyword">while</span> (<span class="keyword">true</span>)&#123;</span><br><span class="line">                clientSocket =  listenSocket.accept();   <span class="comment">//服务器阻塞等待，直到获取并接受到客户端的连接请求</span></span><br><span class="line">                ServerThread serverThread = <span class="keyword">new</span> ServerThread(clientSocket);    <span class="comment">//收到连接请求创建一个新的服务线程</span></span><br><span class="line">                serverThread.setPriority(<span class="number">4</span>);    <span class="comment">//设置线程的优先级，范围为1-10，默认为5</span></span><br><span class="line">                serverThread.start();   <span class="comment">//启动线程</span></span><br><span class="line"></span><br><span class="line">                System.out.println(<span class="string">"已收到一条客户端连接请求，创建一条客户连接线程，线程ID为"</span>+serverThread.getId());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Server server = <span class="keyword">new</span> Server();</span><br><span class="line">        server.serverImpl();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>



<p>创建服务线程类，继承Thread类重写run方法。将原来Server类中，获取输入输出流以读取响应客户端请求的代码，迁移至重写的run方法中。</p>
<p>ServerThread类的代码为：</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="JAVA"><figure class="iseeu highlight /java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.*;</span><br><span class="line"><span class="keyword">import</span> java.net.Socket;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 服务器线程处理类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ServerThread</span> <span class="keyword">extends</span> <span class="title">Thread</span></span>&#123;</span><br><span class="line">    Socket socket = <span class="keyword">null</span>;   <span class="comment">//与本线程相关的Socket</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">//构造方法初始化Socket</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">ServerThread</span><span class="params">(Socket socket)</span></span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.socket = socket;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//重写run方法，自定义线程执行的操作，响应客户端请求</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">//获取输入流，以获取客户端发来的数据</span></span><br><span class="line">            InputStream input = socket.getInputStream();    <span class="comment">//从clientSocket生成输入流。</span></span><br><span class="line">            InputStreamReader inputReader = <span class="keyword">new</span> InputStreamReader(input);   <span class="comment">//将字节流转换为对中文兼容好的字符流</span></span><br><span class="line">            BufferedReader bufferedReader = <span class="keyword">new</span> BufferedReader(inputReader);    <span class="comment">//创建与字符流配套的缓冲流</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">//读取客户端发来的数据</span></span><br><span class="line">            String clientInfo = <span class="keyword">null</span>;</span><br><span class="line">            <span class="keyword">while</span> ((clientInfo = bufferedReader.readLine()) != <span class="keyword">null</span>)  <span class="comment">//循环读取文本行</span></span><br><span class="line">                System.out.println(<span class="string">"客户端数据说："</span> + clientInfo);</span><br><span class="line"></span><br><span class="line">            <span class="comment">//获取输出流</span></span><br><span class="line">            OutputStream output = socket.getOutputStream();   <span class="comment">//获取输出流</span></span><br><span class="line">            OutputStreamWriter outputWriter = <span class="keyword">new</span> OutputStreamWriter(output);   <span class="comment">//将字节流转换为字符流</span></span><br><span class="line">            BufferedWriter bufferedWriter = <span class="keyword">new</span> BufferedWriter(outputWriter);   <span class="comment">//为字符流添加缓冲</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">//响应客户端会话</span></span><br><span class="line">            String replayData = <span class="string">"你好！我是服务器，很高兴认识你"</span>;</span><br><span class="line">            bufferedWriter.write(replayData);   <span class="comment">//写入回复的数据</span></span><br><span class="line">            bufferedWriter.flush();     <span class="comment">//刷新该流的缓冲</span></span><br><span class="line">            System.out.println(<span class="string">"已回复客户端会话："</span> + replayData);</span><br><span class="line"></span><br><span class="line">            <span class="comment">//关闭相关资源</span></span><br><span class="line">            <span class="keyword">if</span> (bufferedReader != <span class="keyword">null</span>)</span><br><span class="line">                bufferedReader.close();</span><br><span class="line">            <span class="keyword">if</span> (socket != <span class="keyword">null</span>)</span><br><span class="line">                socket.close();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>





<h4 id="基于UDP的Socket编程"><a href="#基于UDP的Socket编程" class="headerlink" title="基于UDP的Socket编程"></a>基于UDP的Socket编程</h4><p>​        UDP协议（用户数据报协议）是无连接、不可靠的、无序的。UDP协议以数据报作为数据传输的载体。</p>
<p>​        进行数据传输时，首先需要将要传输的数据定义成数据报（Datagram），在数据报中指明数据所要到达的Socket（主机地址和端口号），然后再将数据报发送出去。</p>
<ul>
<li>DatagramPacket：表示数据报包</li>
<li>DatagramSocket：进行端到端通信的类</li>
</ul>
<p>服务器端实现步骤：</p>
<p>1、创建DatagramSocket，指定端口号。</p>
<p>2、创建DatagramPacket。</p>
<p>3、接受客户端发送的数据信息。</p>
<p>4、读取数据。</p>
<p>客户端实现步骤：</p>
<p>1、定义发送信息。</p>
<p>2、创建DatagramPacket，包含将要发送的信息。</p>
<p>3、创建DatagramSocket。</p>
<p>4、发送数据</p>
<p>基于UDP的Socket通信实例如下（多线程实现逻辑跟上面的例子差不多，这里就不重复操作了）：</p>
<p>服务器端：</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="JAVA"><figure class="iseeu highlight /java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.net.DatagramPacket;</span><br><span class="line"><span class="keyword">import</span> java.net.DatagramSocket;</span><br><span class="line"><span class="keyword">import</span> java.net.InetAddress;</span><br><span class="line"><span class="keyword">import</span> java.net.SocketException;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 服务端，基于UDP的用户登录</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UDPServer</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">serverImpl</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="keyword">int</span> port = <span class="number">8800</span>;</span><br><span class="line">            DatagramSocket socket = <span class="keyword">new</span> DatagramSocket(port);   <span class="comment">//创建服务器端DatagramSocket，指定监听端口</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">//接受数据</span></span><br><span class="line">            <span class="keyword">byte</span>[] data = <span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">1024</span>];   <span class="comment">//创建字节数组，指定接收的数据包大小</span></span><br><span class="line">            DatagramPacket packet = <span class="keyword">new</span> DatagramPacket(data,data.length);   <span class="comment">//创建数据报，用于接收客户端发送的数据</span></span><br><span class="line">            System.out.println(<span class="string">"***服务器端已经启动，等待客户端连接***"</span>);</span><br><span class="line"></span><br><span class="line">            socket.receive(packet);  <span class="comment">//接受客户端发送的数据报，此方法在接收到数据报之前会一直阻塞</span></span><br><span class="line">            String info = <span class="keyword">new</span> String(data,<span class="number">0</span>,packet.getLength());    <span class="comment">//读取数据</span></span><br><span class="line">            System.out.println(<span class="string">"我是服务器，客户端说："</span>+info);</span><br><span class="line"></span><br><span class="line">            <span class="comment">//响应消息</span></span><br><span class="line">            InetAddress clientAddress = packet.getAddress();  <span class="comment">//获取客户端地址</span></span><br><span class="line">            <span class="keyword">int</span> clientPort = packet.getPort();  <span class="comment">//获取客户端端口</span></span><br><span class="line">            <span class="keyword">byte</span>[] replayData = <span class="string">"登录成功，欢迎您！"</span>.getBytes();    <span class="comment">//定义响应数据，并转换为字节类型</span></span><br><span class="line">            DatagramPacket replayPacket = <span class="keyword">new</span> DatagramPacket(replayData,replayData.length,clientAddress,clientPort);  <span class="comment">//创建数据包，并绑定客户端地址</span></span><br><span class="line">            socket.send(replayPacket); <span class="comment">//向客户端响应数据报</span></span><br><span class="line">            System.out.println(<span class="string">"已向客户端响应数据："</span>+<span class="keyword">new</span> String(replayData));</span><br><span class="line"></span><br><span class="line">            socket.close();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (SocketException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        UDPServer server = <span class="keyword">new</span> UDPServer();</span><br><span class="line">        server.serverImpl();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>



<p>客户端：</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="JAVA"><figure class="iseeu highlight /java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.net.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UDPClient</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">clientImpl</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            DatagramSocket socket = <span class="keyword">new</span> DatagramSocket();   <span class="comment">//创建DatagramSocket对象</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">//发送数据</span></span><br><span class="line">            InetAddress address = InetAddress.getByName(<span class="string">"localhost"</span>);   <span class="comment">//定义服务器地址</span></span><br><span class="line">            <span class="keyword">int</span> port = <span class="number">8800</span>;    <span class="comment">//定义服务器端口</span></span><br><span class="line">            <span class="keyword">byte</span>[] data = <span class="string">"用户名：admin;密码：123"</span>.getBytes();    <span class="comment">//定义发送数据，并转换为字节类型</span></span><br><span class="line">            DatagramPacket packet = <span class="keyword">new</span> DatagramPacket(data,data.length,address,port);  <span class="comment">//创建数据包，并绑定服务器地址</span></span><br><span class="line">            socket.send(packet); <span class="comment">//向服务器发送数据报</span></span><br><span class="line">            System.out.println(<span class="string">"已向服务器发送数据："</span>+<span class="keyword">new</span> String(data));</span><br><span class="line"></span><br><span class="line">            <span class="comment">//接受响应消息</span></span><br><span class="line">            <span class="keyword">byte</span>[] replayData = <span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">1024</span>];   <span class="comment">//创建字节数组，指定接收的数据包大小</span></span><br><span class="line">            DatagramPacket serverPacket = <span class="keyword">new</span> DatagramPacket(replayData,replayData.length);   <span class="comment">//创建数据报，用于接收服务器响应的数据</span></span><br><span class="line"></span><br><span class="line">            socket.receive(serverPacket);  <span class="comment">//接受服务器响应的数据报，此方法在接收到数据报之前会一直阻塞</span></span><br><span class="line">            String info = <span class="keyword">new</span> String(replayData,<span class="number">0</span>,serverPacket.getLength());    <span class="comment">//读取数据</span></span><br><span class="line">            System.out.println(<span class="string">"我是客户端，服务端回复说："</span>+info);</span><br><span class="line"></span><br><span class="line">            socket.close();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (UnknownHostException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (SocketException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        UDPClient client = <span class="keyword">new</span> UDPClient();</span><br><span class="line">        client.clientImpl();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>



<h4 id="重点总结"><a href="#重点总结" class="headerlink" title="重点总结"></a>重点总结</h4><h5 id="本章主要内容"><a href="#本章主要内容" class="headerlink" title="本章主要内容"></a>本章主要内容</h5><ul>
<li><p>Socket通信的主要步骤。</p>
</li>
<li><p>基于TCP的Socket通信。</p>
</li>
<li><p>基于UDP的Socket通信。</p>
</li>
<li><p>基于多线程的服务器与多客户端通信。</p>
</li>
</ul>
<h5 id="多线程的优先级："><a href="#多线程的优先级：" class="headerlink" title="多线程的优先级："></a>多线程的优先级：</h5><div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="JAVA"><figure class="iseeu highlight /java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">serverThread.setPriority(<span class="number">4</span>);    <span class="comment">//设置线程的优先级，范围为1-10，默认为5</span></span><br></pre></td></tr></table></figure></div>

<p>未设置优先级可能会导致运行时速度非常慢，可降低优先级。</p>
<h5 id="是否关闭输出流和输入流："><a href="#是否关闭输出流和输入流：" class="headerlink" title="是否关闭输出流和输入流："></a>是否关闭输出流和输入流：</h5><p>​        程序未执行结束时，不能关闭输出流。对于同一个socket的输出流，如果关闭了输出流，则与该输出流关联的Socket也会被关闭，所以一般不用关闭流，最后直接关闭socket即可。</p>
<h5 id="关于数据传输格式："><a href="#关于数据传输格式：" class="headerlink" title="关于数据传输格式："></a>关于数据传输格式：</h5><p>​        上面例子使用TCP通信传输数据，是通过字符串的形式传输的，而实际的应用中，更多是通过将数据封装成对象进行传输的。</p>
<blockquote>
<p>2020年10月27日 完结</p>
</blockquote>
<hr>

      
    </div>
    
      <footer class="article-footer">
        完
      </footer>
    
  </div>
  
    
<nav id="article-nav">
  <div class="article-nav-block">
    
      <a href="/2020/11/29/040.Docker%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/" id="article-nav-newer" class="article-nav-link-wrap">
        <strong class="article-nav-caption"></strong>
        <div class="article-nav-title">
          
            040.Docker 使用指南
          
        </div>
      </a>
    
  </div>
  <div class="article-nav-block">
    
      <a href="/2020/08/19/038.MySQL%E8%BF%90%E7%BB%B4%E7%9F%A5%E8%AF%86%E6%A2%B3%E7%90%86/" id="article-nav-older" class="article-nav-link-wrap">
        <div class="article-nav-title">038.MySQL运维知识梳理</div>
        <strong class="article-nav-caption"></strong>
      </a>
    
  </div>
</nav>

    <link rel="stylesheet" href="/css/gitment.css"> 
<script src="/js/gitment.js"></script>

<div id="gitmentContainer"></div>

<script>
var gitment = new Gitment({
  owner: '',
  repo: '',
  oauth: {
    client_id: '',
    client_secret: '',
  },
})
gitment.render('gitmentContainer')
</script>

  
  
</article>
</section>
        <aside id="sidebar">
  
    <div class="widget-box">
  <div class="avatar-box">
    <img class="avatar" src="/images/avatar.png" title="图片来自网络"></img>
    <h3 class="avatar-name">
      
        似南
      
    </h3>
    <p class="avatar-slogan">
      天空透露着微光，照亮虚无迷惘。
    </p>
  </div>
</div>


  
    
  <div class="widget-box">
    <h3 class="widget-title">分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Web%E5%85%A8%E6%A0%88%E6%9E%B6%E6%9E%84/">Web全栈架构</a><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/Web%E5%85%A8%E6%A0%88%E6%9E%B6%E6%9E%84/Java/">Java</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Web%E5%85%A8%E6%A0%88%E6%9E%B6%E6%9E%84/JavaScript/">JavaScript</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Web%E5%85%A8%E6%A0%88%E6%9E%B6%E6%9E%84/PHP/">PHP</a></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/">MySQL</a></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BB%BC%E5%90%88%E6%8A%80%E6%9C%AF%E6%A0%88/">综合技术栈</a><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BB%BC%E5%90%88%E6%8A%80%E6%9C%AF%E6%A0%88/Git/">Git</a></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%BF%90%E7%BB%B4%E6%8A%80%E6%9C%AF/">运维技术</a><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%BF%90%E7%BB%B4%E6%8A%80%E6%9C%AF/Linux%E7%B3%BB%E7%BB%9F%E8%BF%90%E7%BB%B4/">Linux系统运维</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%BF%90%E7%BB%B4%E6%8A%80%E6%9C%AF/Web%E5%AE%89%E5%85%A8/">Web安全</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%BF%90%E7%BB%B4%E6%8A%80%E6%9C%AF/%E6%95%B0%E9%80%9A/">数通</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%BF%90%E7%BB%B4%E6%8A%80%E6%9C%AF/%E8%99%9A%E6%8B%9F%E5%8C%96/">虚拟化</a></li></ul></li></ul>
    </div>
  </div>


  
    
  <div class="widget-box">
    <h3 class="widget-title">标签云</h3>
    <div class="widget tagcloud">
      <a href="/tags/Docker/" style="font-size: 10px;">Docker</a> <a href="/tags/Hexo/" style="font-size: 10px;">Hexo</a> <a href="/tags/JDBC/" style="font-size: 15px;">JDBC</a> <a href="/tags/JavaGUI/" style="font-size: 10px;">JavaGUI</a> <a href="/tags/MyBatis/" style="font-size: 10px;">MyBatis</a> <a href="/tags/SSH/" style="font-size: 10px;">SSH</a> <a href="/tags/Servlet/" style="font-size: 10px;">Servlet</a> <a href="/tags/Socket/" style="font-size: 10px;">Socket</a> <a href="/tags/Spring/" style="font-size: 10px;">Spring</a> <a href="/tags/SpringBoot/" style="font-size: 15px;">SpringBoot</a> <a href="/tags/SpringCloud/" style="font-size: 10px;">SpringCloud</a> <a href="/tags/Telnet/" style="font-size: 10px;">Telnet</a> <a href="/tags/VLAN/" style="font-size: 15px;">VLAN</a> <a href="/tags/%E4%BC%98%E5%8C%96/" style="font-size: 10px;">优化</a> <a href="/tags/%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/" style="font-size: 10px;">使用指南</a> <a href="/tags/%E5%8D%8F%E8%AE%AE%E5%88%86%E6%9E%90/" style="font-size: 10px;">协议分析</a> <a href="/tags/%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5/" style="font-size: 10px;">增删改查</a> <a href="/tags/%E5%A4%87%E4%BB%BD/" style="font-size: 10px;">备份</a> <a href="/tags/%E5%A4%9A%E7%BA%BF%E7%A8%8B/" style="font-size: 10px;">多线程</a> <a href="/tags/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A2/" style="font-size: 10px;">多表查询</a> <a href="/tags/%E5%AD%A6%E4%B9%A0%E6%8C%87%E5%8D%97/" style="font-size: 20px;">学习指南</a> <a href="/tags/%E6%80%9D%E7%A7%91%E6%95%B0%E9%80%9A/" style="font-size: 15px;">思科数通</a> <a href="/tags/%E6%8E%A5%E5%8F%A3%E8%AF%B7%E6%B1%82/" style="font-size: 10px;">接口请求</a> <a href="/tags/%E6%95%85%E9%9A%9C%E6%8E%92%E9%99%A4/" style="font-size: 10px;">故障排除</a> <a href="/tags/%E6%97%A0%E7%BA%BFWiFi/" style="font-size: 10px;">无线WiFi</a> <a href="/tags/%E6%9D%83%E9%99%90%E6%8E%A7%E5%88%B6/" style="font-size: 10px;">权限控制</a> <a href="/tags/%E7%9B%91%E6%8E%A7/" style="font-size: 10px;">监控</a> <a href="/tags/%E7%AB%AF%E5%8F%A3%E9%85%8D%E7%BD%AE/" style="font-size: 10px;">端口配置</a> <a href="/tags/%E7%B4%A2%E5%BC%95/" style="font-size: 15px;">索引</a> <a href="/tags/%E8%99%9A%E6%8B%9F%E6%9C%BA/" style="font-size: 10px;">虚拟机</a> <a href="/tags/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/" style="font-size: 10px;">读书笔记</a> <a href="/tags/%E8%B7%AF%E7%94%B1%E9%85%8D%E7%BD%AE/" style="font-size: 10px;">路由配置</a> <a href="/tags/%E9%9B%86%E7%BE%A4/" style="font-size: 10px;">集群</a>
    </div>
  </div>

  
    
  <div class="widget-box">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/11/">十一月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">七月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/12/">十二月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/11/">十一月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/10/">十月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/08/">八月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/06/">六月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/05/">五月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/04/">四月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/03/">三月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/01/">一月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/12/">十二月 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/11/">十一月 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/10/">十月 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/08/">八月 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/07/">七月 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">六月 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/05/">五月 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/04/">四月 2019</a></li></ul>
    </div>
  </div>

  
    
  <div class="widget-box">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/11/16/046%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93%E4%BA%8B%E5%8A%A1%E7%9B%B8%E5%85%B3%E7%9F%A5%E8%AF%86%E6%A2%B3%E7%90%86/">046、数据库事务相关知识梳理</a>
          </li>
        
          <li>
            <a href="/2021/11/15/045.%E6%95%B0%E6%8D%AE%E5%BA%93%E7%B4%A2%E5%BC%95%E7%9F%A5%E8%AF%86%E6%A2%B3%E7%90%86/">045.数据库索引知识梳理</a>
          </li>
        
          <li>
            <a href="/2021/11/12/044.%E6%95%B0%E6%8D%AE%E5%BA%93%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A2%E6%96%B9%E5%BC%8F/">044.数据库多表查询方式</a>
          </li>
        
          <li>
            <a href="/2021/11/03/043.Java%20%E5%A4%9A%E7%BA%BF%E7%A8%8B%E6%A6%82%E8%A6%81/">043.Java 多线程概要</a>
          </li>
        
          <li>
            <a href="/2021/07/31/042.SpringCloud%E5%BE%AE%E6%9C%8D%E5%8A%A1%E5%AE%9E%E7%8E%B0/">042.SpringCloud 微服务实现</a>
          </li>
        
      </ul>
    </div>
  </div>

  
      <div class="widget-box">
    <h3 class="widget-title">友链</h3>
    <div class="widget">
      
        <a style="display: block;" href="https://yiluyanxia.github.io/" title target='_blank'
        >一路眼瞎</a>
      
    </div>
  </div>

  
</aside>
      </div>
      <footer id="footer">
  <div class="foot-box global-width">
    &copy; 2022 FaithSinan &nbsp;&nbsp;
    驱动于 <a href="http://hexo.io/" target="_blank">Hexo</a>
    &nbsp;|&nbsp;基于 <a href="https://github.com/yiluyanxia/hexo-theme-antiquity" target="_blank" rel="noopener">antiquity</a>
    <br>
    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span id="busuanzi_container_site_pv">不蒜子告之   阁下是第<span id="busuanzi_value_site_pv"></span>个访客</span>
  </div>
</footer>
      <script src="https://code.jquery.com/jquery-2.0.3.min.js"></script>
<script>
if (!window.jQuery) {
var script = document.createElement('script');
script.src = "/js/jquery-2.0.3.min.js";
document.body.write(script);
}
</script>

  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




    </div>
    <nav id="mobile-nav" class="mobile-nav-box">
  <div class="mobile-nav-img mobile-nav-top"></div>
  
    <a href="/" class="mobile-nav-link">首页</a>
  
    <a href="/archives" class="mobile-nav-link">归档</a>
  
  <div class="mobile-nav-img  mobile-nav-bottom"></div>
</nav>    
  </div>
</body>
</html>